package net.hands.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

import net.hands.common.logger.Log;
import net.hands.common.logger.LogFactory;

/**
 * 说明:线程操作台
 * <br>
 * <p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public class ConsoleThread extends Thread {
	/**
	 * 
	* 说明:初始化线程操作台
	* @update 2012-9-24 上午10:23:03
	 */
	public static void doInit() {
		if ((System.in != null) && (System.out != null)) {
			final ConsoleThread console = new ConsoleThread(System.in);
			console.setPrintStream(System.out);
			console.start();
		}
	}
   /**
    * 命令集合
    */
	private final ICommand[] commands = new ICommand[] { new QuitCommand(), new GcCommand() };
   /**
    * 缓存读取
    */
	private final BufferedReader reader;
   /**
    * 打印流
    */
	private PrintStream printStream;
   /**
    * 
   * Constructor Method  
   * @update 2012-9-24 上午10:37:18  
   * @param inputStream 输入流
    */
	public ConsoleThread(final InputStream inputStream) {
		setDaemon(true);
		reader = new BufferedReader(new InputStreamReader(inputStream));
	}
   /**
   * 说明:获得打印流
   * @return PrintStream 
   * @update 2012-9-24 上午10:37:39
    */
	public PrintStream getPrintStream() {
		return printStream;
	}
  
	private final Log log = LogFactory.getLogger(ConsoleThread.class);
   /**
    * 
   (non-Javadoc)  
   * @see java.lang.Thread#run()  
   * @update 2012-9-24 上午10:38:06
    */
	@Override
	public void run() {
		while (true) {
			try {
				final String text = reader.readLine();//读取命令行
				if (!StringUtils.hasText(text)) {
					break;
				}
				for (final ICommand command : commands) {//循环执行命令
					if (command.execute(text.toLowerCase())) {
						break;
					}
				}
			} catch (final IOException e) {
				log.error(e);
				break;
			}
		}
	}
   /**
   * 说明:设置打印流
   * @param printStream 打印流
   * @update 2012-9-24 上午10:40:54
    */
	public void setPrintStream(final PrintStream printStream) {
		this.printStream = printStream;
	}
   /**
   * 说明:命令接口 
   * @update 2012-9-24 上午10:23:59
    */
	interface ICommand {
		/**
		* 说明:执行命令
		* @param command 命令行
		* @return boolean
		* @update 2012-9-24 上午10:24:13
		 */
		boolean execute(String command);
	}
   /**
    * 
   * 说明:停止或退出命令
   * @update 2012-9-24 上午10:24:45
    */
	class QuitCommand implements ICommand {
     
		@Override
		public boolean execute(final String command) {
			if (command.equals("quit") || command.equals("exit")) {
				System.exit(0);
				return true;
			} else {
				return false;
			}
		}
	}
   /**
    * 
   * 说明:垃圾回收命令
   * @update 2012-9-24 上午10:25:23
    */
	class GcCommand implements ICommand {

		@Override
		public boolean execute(final String command) {
			if (command.equals("gc")) {
				String size = IoUtils.toFileSize(Runtime.getRuntime().totalMemory());
				getPrintStream().println("total memory: " + size);
				size = IoUtils.toFileSize(Runtime.getRuntime().freeMemory());
				getPrintStream().println("free memory before gc: " + size);
				getPrintStream().println();
				System.gc();
				System.out.println("garbage collection ok.");
				getPrintStream().println();
				size = IoUtils.toFileSize(Runtime.getRuntime().freeMemory());
				getPrintStream().println("free memory after gc: " + size);
				return true;
			} else {
				return false;
			}
		}
	}
}
